// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "./gameData";
import {petPosition} from "./petMediator";

const {ccclass, property} = cc._decorator;

let petSkinSpine = ["cw01","cw02","cw03","cw04","cw05","cw06","cw07","cw08","cw09","cw10","cw11","cw12","cw13","cw14","cw15","cw16","cw17","cw18","cw19","cw20","cw21","cw22","cw23","cw24","cw25","cw26","cw27","cw28","cw29","cw30","cw31"];

let maxLength = 1;

@ccclass
export default class petGroupMediator extends cc.Component {

    @property({displayName : "游戏界面宠物逻辑"})
    gameView : boolean = false;

    onLoad () {
        let num = gameData.judgeWearPetNumber(gameData.curRankTitle);
        // let num = 2;
        if(num < gameData.curUseSkin.length){
            gameData.curUseSkin.splice(num,gameData.curUseSkin.length - num);
            gameData.saveLastPetSkin();
        }
        if(this.gameView){
            let templateNode = this.node.getChildByName("pet1");
            //初始化生成宠物的逻辑
            for(let i = 0 ; i < 2 ; i++){
                let node = cc.instantiate(templateNode);
                node.getComponent("petMediator").curPetPosition = (1 + i);
                node.name = "pet" + (2 + i);
                node.parent = this.node;
                node.y = templateNode.y;
                node.x = templateNode.x - 150 * (i + 1);
            }
            this.showGamePet();
            cc.game.on("resetPet",()=>{
                this.showGamePet();
            });
            cc.game.on("showMorePet",(res)=>{
                this.node.getChildByName("pet3").active = true;
                this.loadPetSkin(this.node.getChildByName("pet3").getChildByName("spine"),petSkinSpine[res]);
            });
        }else{
            this.showMainPet();
            cc.game.on("resetPet",()=>{
                this.showMainPet();
            })
        }
    }

    protected onDestroy(): void {
        cc.game.off("resetPet");
        cc.game.off("showMorePet");
    }

    showMainPet(){
        if(gameData.getPetCurSkin().length == 0){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
        }else if(gameData.getPetCurSkin().length == 1){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1"),petSkinSpine[gameData.getPetCurSkin()[0]]);
        }else if(gameData.getPetCurSkin().length == 2){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2"),petSkinSpine[gameData.getPetCurSkin()[1]]);
        }else if(gameData.getPetCurSkin().length == 3){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2"),petSkinSpine[gameData.getPetCurSkin()[1]]);
            this.loadPetSkin(this.node.getChildByName("pet3"),petSkinSpine[gameData.getPetCurSkin()[2]]);
        }else if(gameData.getPetCurSkin().length == 4){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2"),petSkinSpine[gameData.getPetCurSkin()[1]]);
            this.loadPetSkin(this.node.getChildByName("pet3"),petSkinSpine[gameData.getPetCurSkin()[2]]);
            this.loadPetSkin(this.node.getChildByName("pet4"),petSkinSpine[gameData.getPetCurSkin()[3]]);
        }else if(gameData.getPetCurSkin().length == 5){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2"),petSkinSpine[gameData.getPetCurSkin()[1]]);
            this.loadPetSkin(this.node.getChildByName("pet3"),petSkinSpine[gameData.getPetCurSkin()[2]]);
            this.loadPetSkin(this.node.getChildByName("pet4"),petSkinSpine[gameData.getPetCurSkin()[3]]);
            this.loadPetSkin(this.node.getChildByName("pet5"),petSkinSpine[gameData.getPetCurSkin()[4]]);
        }else if(gameData.getPetCurSkin().length == 6){
            for(let i = 1 ; i <1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2"),petSkinSpine[gameData.getPetCurSkin()[1]]);
            this.loadPetSkin(this.node.getChildByName("pet3"),petSkinSpine[gameData.getPetCurSkin()[2]]);
            this.loadPetSkin(this.node.getChildByName("pet4"),petSkinSpine[gameData.getPetCurSkin()[3]]);
            this.loadPetSkin(this.node.getChildByName("pet5"),petSkinSpine[gameData.getPetCurSkin()[4]]);
            this.loadPetSkin(this.node.getChildByName("pet6"),petSkinSpine[gameData.getPetCurSkin()[5]]);
        }else if(gameData.getPetCurSkin().length == 7){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2"),petSkinSpine[gameData.getPetCurSkin()[1]]);
            this.loadPetSkin(this.node.getChildByName("pet3"),petSkinSpine[gameData.getPetCurSkin()[2]]);
            this.loadPetSkin(this.node.getChildByName("pet4"),petSkinSpine[gameData.getPetCurSkin()[3]]);
            this.loadPetSkin(this.node.getChildByName("pet5"),petSkinSpine[gameData.getPetCurSkin()[4]]);
            this.loadPetSkin(this.node.getChildByName("pet6"),petSkinSpine[gameData.getPetCurSkin()[5]]);
            this.loadPetSkin(this.node.getChildByName("pet7"),petSkinSpine[gameData.getPetCurSkin()[6]]);
        }
    }


    showGamePet(){
        if(gameData.getPetCurSkin().length == 0){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
        }else if(gameData.getPetCurSkin().length == 1){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[0]]);
        }else if(gameData.getPetCurSkin().length == 2){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[1]]);
        }else if(gameData.getPetCurSkin().length == 3){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[1]]);
            this.loadPetSkin(this.node.getChildByName("pet3").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[2]]);
        }else if(gameData.getPetCurSkin().length == 4){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[1]]);
            this.loadPetSkin(this.node.getChildByName("pet3").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[2]]);
            this.loadPetSkin(this.node.getChildByName("pet4").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[3]]);
        }else if(gameData.getPetCurSkin().length == 5){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[1]]);
            this.loadPetSkin(this.node.getChildByName("pet3").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[2]]);
            this.loadPetSkin(this.node.getChildByName("pet4").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[3]]);
            this.loadPetSkin(this.node.getChildByName("pet5").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[4]]);
        }else if(gameData.getPetCurSkin().length == 6){
            for(let i = 1 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[1]]);
            this.loadPetSkin(this.node.getChildByName("pet3").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[2]]);
            this.loadPetSkin(this.node.getChildByName("pet4").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[3]]);
            this.loadPetSkin(this.node.getChildByName("pet5").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[4]]);
            this.loadPetSkin(this.node.getChildByName("pet6").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[5]]);
        }else if(gameData.getPetCurSkin().length == 7){
            for(let i = 2 ; i < 1 + maxLength ; i++){
                if(i < gameData.getPetCurSkin().length + 1){
                    this.node.getChildByName("pet" + i).active = true;
                }else{
                    this.node.getChildByName("pet" + i).active = false;
                }
            }
            this.loadPetSkin(this.node.getChildByName("pet1").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.node.getChildByName("pet2").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[1]]);
            this.loadPetSkin(this.node.getChildByName("pet3").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[2]]);
            this.loadPetSkin(this.node.getChildByName("pet4").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[3]]);
            this.loadPetSkin(this.node.getChildByName("pet5").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[4]]);
            this.loadPetSkin(this.node.getChildByName("pet6").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[5]]);
            this.loadPetSkin(this.node.getChildByName("pet7").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[6]]);
        }
    }

    loadPetSkin(node,name){
        cc.loader.loadRes("anim/" + name,sp.SkeletonData,(error, resource) =>{
            let spk = node.getComponent(sp.Skeleton);
            spk.skeletonData = resource;
            node.active = true;
            let order = petSkinSpine.indexOf(name);
            if(gameData.newYearPetUseStatus[order] == 1){
                spk.setSkin("pf02");
            }else{
                spk.setSkin("pf01");
            }
            if(name == "cw29"){
                node.scale = 0.4;
            }
            spk.setAnimation(0,"daiji",true);
        })
    }
}
